<?php

include 'include/init.php';

$method=$_POST['method'];
$uid=$_POST['u'];


//如果是获取该好友的添加好友权限
if($method=='getdef')
{
	$frdid=$_POST['f'];

	//先判断添加的该名好友是否已经加了我，若果加了，则权限按0算
	$frd=new frdModel();
	$isfrd=$frd->isfrd($frdid,$uid);
	if($isfrd==1)
	{
		echo json_encode(0);
		exit;
	}
	//没有加的话按deftype算
	$sa=new setting_addfrdModel();
	$def=$sa->getDef($frdid);
	if($def)
	{
		if($def['deftype']==0)
		{
			echo json_encode(0);
		}
		else if($def['deftype']==1)
		{
			echo json_encode(1);
		}
		else if($def['deftype']==2)
		{
			$list=array();
			$v=explode(';',$def['question']);
			$list['said']=$def['said'];
			$list['uid']=$def['uid'];
			$list['deftype']=$def['deftype'];
			$list['question']=$v[0];
			$list['answer']=$v[1];

			echo json_encode($list);

		}
		else if($def['deftype']==3)
		{
			$list=array();
			$v=explode(';',$def['question']);
			$list['said']=$def['said'];
			$list['uid']=$def['uid'];
			$list['deftype']=$def['deftype'];
			$list['question']=$v;

			echo json_encode($list);
		}
		else
		{
			echo 'no';
		}
		
	}
	else
	{
		echo 'no';
	}
	exit;
}else if($method=='getgrp')
{
	//获取用户分组
	$grp=new groupModel();
	$gs=$grp->getGrps($uid);
	if($gs)
	{
		echo json_encode($gs);
	}
	else
	{
		echo json_encode("no");
	}
	exit;
}
else if($method=="sendaddfrd")
{
	$data=array();
	$validate=$_POST['v']+0;
	$data['deftype']=$validate;
	$data['frdid']=$_POST['fid'];
	$data['togrp']=$_POST['togrp'];
	$data['acc_to_space']=$_POST['aon'];
	$bakname=$_POST['bakname'];
	
	//再次检查是否已为好友
	$frd=new frdModel();

	$isfrd=$frd->isfrd($uid,$data['frdid']);
	if($isfrd==-1||$isfrd==1)
	{
		echo json_encode("no");
		exit;
	}

	$user=new userModel();


	if($validate==0)
	{
		//添加好友为真
		$data['add_or_not']=1;
		$data['done']=1;
		//添加好友信息
		$afm=new addfrdmsgModel();
		if($afm->add($uid,$data))
		{
			//插入新的frd
			
			$newfrd=array();
			$newfrd['frdid']=$data['frdid'];
			$newfrd['gid']=$data['togrp'];
			$newfrd['frdname']=$bakname;

			if($frd->add($uid,$newfrd))
			{
				//获取好友所有信息
				$frdmsg=$user->getbyid($newfrd['frdid']);
				$frdmsg['frdname']=$bakname;
				echo json_encode($frdmsg);
			}
			else
			{
				echo json_encode("no");
			}
			
		}
		else
		{
			echo json_encode("no");
		}
		exit;

	}else if($validate==1)
	{
		$data['answer']=$_POST['answer'];
		//添加好友信息
		$afm=new addfrdmsgModel();
		if($afm->add($uid,$data))
		{
			echo json_encode("wait");
		}
		else
		{
			echo json_encode("no");
		}
		exit;

	}else if($validate==2||$validate==3)
	{
		$data['question']=$_POST['question'];
		$data['answer']=$_POST['answer'];
		//添加好友信息
		$afm=new addfrdmsgModel();
		if($afm->add($uid,$data))
		{
			echo json_encode("wait");
		}
		else
		{
			echo json_encode("no");
		}
		exit;


	}else
	{
		echo json_encode("no");
		exit;
	}
}else if($method=="addgrp")
{
	$gname=$_POST['gname'];
	$grp=new groupModel();

	$add=$grp->add($uid,$gname);
	if($add)
	{
		echo $add;
	}
	else
	{
		echo 'no';
	}
	exit;
}